<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_setup_detail_umk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_setup_detail_umk_model', 'DetailUMK');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Detail Item UMK";
		$data['cost_center_data']		= json_encode($this->Combo->SWCostCenter());
		
		
		viewByTemplate('keu_setup_detail_umk/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->DetailUMK->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("nama_detail_umk") == '') {
			$error->Add("nama_detail_umk", "Nama Item", $tpl_empty);
		}
		
		if (post("kd_cost_center") == '') {
			$error->Add("kd_cost_center", "Cost Center", $tpl_empty);
		}
		
		
		if (post("id_coa") == '') {
			$error->Add("id_coa", "COA", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_detail_umk','nama_detail_umk', 'kd_cost_center', 'id_coa'));
				
				
				if (post("mode") == "create")
				{
					$ls_genid = 'I'.str_pad($this->get_data("SELECT NVL((SELECT MAX(SUBSTR(KD_DETAIL_UMK,2,5)) FROM KEU_SETUP_DETAIL_UMK),0)+1 NoUrut FROM DUAL"),5,'0',STR_PAD_LEFT);
					$data['kd_detail_umk']	= $ls_genid;
					$data['create_date']	= NOW;
					$data['create_user']	= USERID;

					$this->DetailUMK->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.kd_report ?? ""));

				}
				else if (post("mode") == "edit")
				{
					$data['modify_date']	= NOW;
					$data['modify_user']	= USERID;
					
					$this->DetailUMK->Update($data, 
										array('KD_DETAIL_UMK'	=> $data['kd_detail_umk']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.kd_report` ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kd_detail_umk	= get('kd_detail_umk');
		
		if ($kd_detail_umk != "")
		{
			// Delete Single
			$ls_result = $this->DetailUMK->DeleteSingle($kd_detail_umk);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->DetailUMK->DeleteSingle($row['kd_detail_umk']);
			}
		}
		
		echo $ls_result;
	}
}
?>